'''
提升树
'''
import scipy.io as scio
import datetime
import numpy as np
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn import metrics


# 读取mat文件
def readMat(matPath):
    return scio.loadmat(matPath)


# 加载数据集
matPath = 'dataSet20180403-K10.mat'
dataSet = readMat(matPath)
print('数据集读取完成')

# 读取数据集
test = np.array(dataSet['test'])
testX = np.array(dataSet['testX'])
testY = np.array(dataSet['testY']).T[0]
train = np.array(dataSet['train'])
trainX = np.array(dataSet['trainX'])
trainY = np.array(dataSet['trainY']).T[0]
print('数据集读取完成')

# Boosting Tree准备
paraResult = []
maxDepthArrary = [10, 20, 30, 40, 50, 80, 100]
nEstimatorsArrary = [10, 50, 100, 300, 500]
learningRateArrary = [0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2.0]
for maxDepth in maxDepthArrary:
    for nEstimators in nEstimatorsArrary:
        for learningRate in learningRateArrary:
            startTime = datetime.datetime.now()
            print(
                'maxDepth:' + str(maxDepth) + ',nEstimators:' + str(nEstimators) + ',learningRate:' + str(learningRate))
            clf = AdaBoostClassifier(DecisionTreeClassifier(max_depth=maxDepth),
                                     algorithm="SAMME",
                                     n_estimators=nEstimators, learning_rate=learningRate)
            clf.fit(trainX, trainY)
            score = clf.score(trainX, trainY)
            print('训练精度：' + str(score))
            score = clf.score(testX, testY)
            print('测试精度：' + str(score))
            predictY = clf.predict(testX)
            print('召回率：' + str(metrics.recall_score(testY, predictY)))
            confusion_matrix = metrics.confusion_matrix(testY, predictY)
            print("混淆矩阵：\n %s" % confusion_matrix)
            TP = confusion_matrix[1][1]
            TN = confusion_matrix[0][0]
            FN = confusion_matrix[1][0]
            FP = confusion_matrix[0][1]
            accuracy = (TP + TN) / (TP + TN + FN + FP)
            Specitivity = TN / (TN + FP)
            Sensitivity = TP / (TP + FN)
            print("准确度： %s" % accuracy)
            print("特异度： %s" % Specitivity)
            print("敏感度： %s" % Sensitivity)

            paraResultItem = {}
            paraResultItem['maxDepth'] = maxDepth
            paraResultItem['nEstimators'] = nEstimators
            paraResultItem['learningRate'] = learningRate
            paraResultItem['score'] = score
            paraResult.append(paraResultItem)
            endTime = datetime.datetime.now()
            print(endTime - startTime)
print('BT结束')
